{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "\n",
    "from keras.preprocessing import sequence\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout, Activation\n",
    "from keras.layers import Embedding\n",
    "from keras.layers import Conv1D, GlobalMaxPooling1D\n",
    "from keras.datasets import imdb\n",
    "\n",
    "# max number of features\n",
    "max_features = 20000\n",
    "# max sentence lenght\n",
    "max_sentence_len = 80  \n",
    "# dimensions in the embedding space\n",
    "embedding_dims = 50\n",
    "# batch size\n",
    "batch_size = 32\n",
    "# filters used in Conv!D\n",
    "filters = 250\n",
    "# size of the kernels in Conv!D\n",
    "kernel_size = 3\n",
    "# hidden dimensions for MLP\n",
    "hidden_dims = 250\n",
    "# training epocs\n",
    "epochs = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25000 train sequences\n",
      "25000 test sequences\n",
      "Pad sequences (samples x time)\n",
      "x_train shape: (25000, 80)\n",
      "x_test shape: (25000, 80)\n"
     ]
    }
   ],
   "source": [
    "(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)\n",
    "print(len(x_train), 'train sequences')\n",
    "print(len(x_test), 'test sequences')\n",
    "\n",
    "print('Pad sequences (samples x time)')\n",
    "x_train = sequence.pad_sequences(x_train, maxlen=max_sentence_len)\n",
    "x_test = sequence.pad_sequences(x_test, maxlen=max_sentence_len)\n",
    "print('x_train shape:', x_train.shape)\n",
    "print('x_test shape:', x_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Build model...\n"
     ]
    }
   ],
   "source": [
    "print('Build model...')\n",
    "model = Sequential()\n",
    "model.add(Embedding(max_features, embedding_dims))\n",
    "model.add(Dropout(0.2))\n",
    "model.add(Conv1D(filters,\n",
    "                 kernel_size,\n",
    "                 padding='valid',\n",
    "                 activation='relu',\n",
    "                 strides=1))\n",
    "# we use max pooling:\n",
    "model.add(GlobalMaxPooling1D())\n",
    "model.add(Dense(hidden_dims))\n",
    "model.add(Dropout(0.2))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Dense(1))\n",
    "model.add(Activation('sigmoid'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 25000 samples, validate on 25000 samples\n",
      "Epoch 1/20\n",
      "25000/25000 [==============================] - 35s - loss: 0.4584 - acc: 0.7682 - val_loss: 0.3564 - val_acc: 0.8437\n",
      "Epoch 2/20\n",
      "25000/25000 [==============================] - 34s - loss: 0.2689 - acc: 0.8885 - val_loss: 0.3573 - val_acc: 0.8464\n",
      "Epoch 3/20\n",
      "25000/25000 [==============================] - 35s - loss: 0.1481 - acc: 0.9444 - val_loss: 0.4275 - val_acc: 0.8397\n",
      "Epoch 4/20\n",
      "25000/25000 [==============================] - 33s - loss: 0.0687 - acc: 0.9752 - val_loss: 0.5693 - val_acc: 0.8298\n",
      "Epoch 5/20\n",
      "25000/25000 [==============================] - 35s - loss: 0.0337 - acc: 0.9880 - val_loss: 0.6721 - val_acc: 0.8274\n",
      "Epoch 6/20\n",
      "25000/25000 [==============================] - 35s - loss: 0.0240 - acc: 0.9924 - val_loss: 0.7733 - val_acc: 0.8238\n",
      "Epoch 7/20\n",
      "25000/25000 [==============================] - 35s - loss: 0.0229 - acc: 0.9920 - val_loss: 0.8571 - val_acc: 0.8218\n",
      "Epoch 8/20\n",
      "25000/25000 [==============================] - 34s - loss: 0.0180 - acc: 0.9940 - val_loss: 0.9327 - val_acc: 0.8190\n",
      "Epoch 9/20\n",
      "25000/25000 [==============================] - 34s - loss: 0.0150 - acc: 0.9951 - val_loss: 0.9652 - val_acc: 0.8185\n",
      "Epoch 10/20\n",
      "25000/25000 [==============================] - 35s - loss: 0.0140 - acc: 0.9955 - val_loss: 0.8886 - val_acc: 0.8228\n",
      "Epoch 11/20\n",
      "25000/25000 [==============================] - 33s - loss: 0.0131 - acc: 0.9957 - val_loss: 0.9696 - val_acc: 0.8197\n",
      "Epoch 12/20\n",
      "25000/25000 [==============================] - 32s - loss: 0.0111 - acc: 0.9963 - val_loss: 1.0649 - val_acc: 0.8238\n",
      "Epoch 13/20\n",
      "25000/25000 [==============================] - 32s - loss: 0.0101 - acc: 0.9963 - val_loss: 1.0606 - val_acc: 0.8217\n",
      "Epoch 14/20\n",
      "25000/25000 [==============================] - 32s - loss: 0.0103 - acc: 0.9968 - val_loss: 1.1543 - val_acc: 0.8050\n",
      "Epoch 15/20\n",
      "25000/25000 [==============================] - 34s - loss: 0.0097 - acc: 0.9967 - val_loss: 1.1228 - val_acc: 0.8113\n",
      "Epoch 16/20\n",
      "25000/25000 [==============================] - 35s - loss: 0.0109 - acc: 0.9961 - val_loss: 1.0900 - val_acc: 0.8123\n",
      "Epoch 17/20\n",
      "25000/25000 [==============================] - 35s - loss: 0.0056 - acc: 0.9978 - val_loss: 1.2230 - val_acc: 0.8125\n",
      "Epoch 18/20\n",
      "25000/25000 [==============================] - 35s - loss: 0.0061 - acc: 0.9976 - val_loss: 1.2706 - val_acc: 0.8170\n",
      "Epoch 19/20\n",
      "25000/25000 [==============================] - 32s - loss: 0.0083 - acc: 0.9972 - val_loss: 1.2003 - val_acc: 0.8109\n",
      "Epoch 20/20\n",
      "25000/25000 [==============================] - 33s - loss: 0.0067 - acc: 0.9976 - val_loss: 1.2917 - val_acc: 0.8105\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x11d897290>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.compile(loss='binary_crossentropy',\n",
    "              optimizer='adam',\n",
    "              metrics=['accuracy'])\n",
    "model.fit(x_train, y_train,\n",
    "          batch_size=batch_size,\n",
    "          epochs=epochs,\n",
    "          validation_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
